﻿using OpenCvSharp.Extensions;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Security.Cryptography;
using Size = OpenCvSharp.Size;
using Tesseract;
using Point = OpenCvSharp.Point;
using Rect = OpenCvSharp.Rect;
using static System.Net.Mime.MediaTypeNames;

namespace GUI4OpenCV.Helpers
{
    internal class DepictImage
    {
        public static Bitmap ImageDepict(Bitmap bitmap)
        {
            //using var src = bitmap.ToMat();
            //using var srcLab = src.CvtColor(ColorConversionCodes.BGR2Lab);//转换到Lab空间有更好的效果

            Mat mat1 = bitmap.ToMat();
            Mat mat2 = new Mat();

            // 为灰度图
            Cv2.CvtColor(mat1, mat2, ColorConversionCodes.BGR2GRAY);

            //二值化 阈值 手动阈值
            // Cv2.Threshold(gray, binary, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
            Cv2.Threshold(mat2, mat2, 125, 255, ThresholdTypes.Binary);


            Mat gauss = new Mat();
            Mat average = new Mat();

            //均值模糊
            //             Cv2.Blur(bitmap.ToMat(), blur, new OpenCvSharp.Size(width, heigth));
            Cv2.Blur(mat2, average, new OpenCvSharp.Size(3, 3), new OpenCvSharp.Point(-1, -1), BorderTypes.Default);

            //高斯模糊
            Cv2.GaussianBlur(mat2, gauss, new OpenCvSharp.Size(5, 5), 0);

            Mat canny = new Mat();

            //Canny边缘检测
            Cv2.Canny(gauss, canny, 5, 90, 3, false);

            ////霍夫圆检测
            var circles = ImageTransformHelper.HouphLineTransferCurve(mat1.ToBitmap());


            ////Mat circles = new Mat();
            //var circles = Cv2.HoughCircles(canny, HoughModes.Gradient, 1,
            //            1200, 100, 30, 200, 500);

            //return canny.ToBitmap();
            ////创建一个掩膜
            //Mat mask = new Mat();
            //mask = mat1;
            //for (int i = 0; i < circles.Count(); i++)
            //{
            //    Cv2.Circle(mask, (int)circles[i].Center.X, (int)circles[i].Center.Y, (int)circles[i].Radius, new Scalar(255, 255, 255), -1, LineTypes.AntiAlias);
            //    ////float[] info = new float[3];
            //    ////circles.get(0, i, info);
            //    //double[] info = circles.get(0, i);
            //    //Point center = new Point((int)info[0], (int)info[1]);
            //    //int radius = (int)info[2];

            //    ////                CircleSegment[] cs = Cv2.HoughCircles(m2, HoughModes.Gradient, 1, 80, 70, 30, 10, 60);
            //    ////src.CopyTo(dst);
            //    ////// Vec3d vec = new Vec3d();
            //    ////for (int i = 0; i < cs.Count(); i++)
            //    ////{
            //    ////    //画圆
            //    ////    Cv2.Circle(dst, (int)cs[i].Center.X, (int)cs[i].Center.Y, (int)cs[i].Radius, new Scalar(0, 0, 255), 2, LineTypes.AntiAlias);
            //    ////    //加强圆心显示
            //    ////    Cv2.Circle(dst, (int)cs[i].Center.X, (int)cs[i].Center.Y, 3, new Scalar(0, 0, 255), 2, LineTypes.AntiAlias);
            //    ////}
            //    ////return dst.ToBitmap();
            //    ////绘制圆时候 thickness参数为-1时候 为填充
            //    ////当第一个参数为mat1 当前图片绘制在原图上
            //    //Cv2.Circle(mask, center, radius, new Scalar(255, 255, 255), -1, 8, 0);
            //}

            return circles;
        }


        public static Bitmap ImageDepict2(Bitmap bitmap)
        {
            //using var src = bitmap.ToMat();
            //using var srcLab = src.CvtColor(ColorConversionCodes.BGR2Lab);//转换到Lab空间有更好的效果

            Mat mat1 = bitmap.ToMat();
            Mat mat2 = new Mat();

            // 为灰度图
            Cv2.CvtColor(mat1, mat2, ColorConversionCodes.BGR2GRAY);

            //二值化 阈值 手动阈值
            // Cv2.Threshold(gray, binary, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
            Cv2.Threshold(mat2, mat2, 125, 255, ThresholdTypes.Binary);


            Mat gauss = new Mat();
            Mat average = new Mat();

            //均值模糊
            //             Cv2.Blur(bitmap.ToMat(), blur, new OpenCvSharp.Size(width, heigth));
            Cv2.Blur(mat2, average, new OpenCvSharp.Size(3, 3), new OpenCvSharp.Point(-1, -1), BorderTypes.Default);

            //高斯模糊
            Cv2.GaussianBlur(mat2, gauss, new OpenCvSharp.Size(5, 5), 0);

            Mat canny = new Mat();

            //Canny边缘检测
            Cv2.Canny(gauss, canny, 5, 90, 3, false);

            ////霍夫圆检测
            var circles = ImageTransformHelper.HouphLineTransferCurve(mat1.ToBitmap());


            ////Mat circles = new Mat();
            //var circles = Cv2.HoughCircles(canny, HoughModes.Gradient, 1,
            //            1200, 100, 30, 200, 500);

            //return canny.ToBitmap();
            ////创建一个掩膜
            //Mat mask = new Mat();
            //mask = mat1;
            //for (int i = 0; i < circles.Count(); i++)
            //{
            //    Cv2.Circle(mask, (int)circles[i].Center.X, (int)circles[i].Center.Y, (int)circles[i].Radius, new Scalar(255, 255, 255), -1, LineTypes.AntiAlias);
            //    ////float[] info = new float[3];
            //    ////circles.get(0, i, info);
            //    //double[] info = circles.get(0, i);
            //    //Point center = new Point((int)info[0], (int)info[1]);
            //    //int radius = (int)info[2];

            //    ////                CircleSegment[] cs = Cv2.HoughCircles(m2, HoughModes.Gradient, 1, 80, 70, 30, 10, 60);
            //    ////src.CopyTo(dst);
            //    ////// Vec3d vec = new Vec3d();
            //    ////for (int i = 0; i < cs.Count(); i++)
            //    ////{
            //    ////    //画圆
            //    ////    Cv2.Circle(dst, (int)cs[i].Center.X, (int)cs[i].Center.Y, (int)cs[i].Radius, new Scalar(0, 0, 255), 2, LineTypes.AntiAlias);
            //    ////    //加强圆心显示
            //    ////    Cv2.Circle(dst, (int)cs[i].Center.X, (int)cs[i].Center.Y, 3, new Scalar(0, 0, 255), 2, LineTypes.AntiAlias);
            //    ////}
            //    ////return dst.ToBitmap();
            //    ////绘制圆时候 thickness参数为-1时候 为填充
            //    ////当第一个参数为mat1 当前图片绘制在原图上
            //    //Cv2.Circle(mask, center, radius, new Scalar(255, 255, 255), -1, 8, 0);
            //}

            return circles;

            //return circles.ToBitmap();
        }
    }
}
